package com.atguigu.ucenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.servicebase.exceptionhandler.SelfExceptionHandler;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.mapper.MemberMapper;
import com.atguigu.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.SocketUtils;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-13
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 登录
     * @param member
     * @return
     */
    @Override
    public String login(Member member) {

        //手机号存在，密码存在，且没有被禁用
        if(member==null||StringUtils.isEmpty(member.getMobile())||StringUtils.isEmpty(member.getPassword()))
        {
            throw new SelfExceptionHandler("登录失败",20001);
        }


        //手机号是否存在.
        String mobile = member.getMobile();
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);

        Member selectOne = baseMapper.selectOne(wrapper);
        if(selectOne==null)
        {
            throw new SelfExceptionHandler("登陆失败,该账号未注册,请先注册",20001);
        }

        //判断用户是否禁用
        if(selectOne.getIsDisabled()==true)
        {
            throw new SelfExceptionHandler("该用户已经被禁用",20001);
        }

        //密码先经过md5加密，然后再比对
        String encrypt = MD5.encrypt(member.getPassword());
        if(selectOne.getPassword().equals(encrypt))
        {
            //登录成功，返回token
            String jwtToken = JwtUtils.getJwtToken(selectOne.getId(), selectOne.getNickname());
            return  jwtToken;

        }else{
            //密码错误
            throw new SelfExceptionHandler("密码错误，登陆失败，请重试",20001);
        }
    }

    /**
     * 注册
     * @param registerVo
     * @return
     */
    @Override
    public void register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();//昵称
        String mobile = registerVo.getMobile();//手机号
        String password = MD5.encrypt(registerVo.getPassword());//密码
        System.out.println("注册的密码是+"+password);
        String code = registerVo.getCode();//验证码


        //redis中用的手机号作为key,code作为value存储验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!redisCode.equals(code))
        {
            throw  new SelfExceptionHandler("注册失败，验证码错误",20001);
        }

        //手机号不能重复
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count!=0)
        {
            throw  new SelfExceptionHandler("该用户已经存在",20001);
        }

        //数据添加到数据库中
        Member member = new Member();
        //设置属性
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(password);
        member.setIsDisabled(false);
        member.setAvatar("https://guli-education-10.oss-cn-chengdu.aliyuncs.com/teacher/ym1.jpg");
        baseMapper.insert(member);
    }

    /**
     * 扫码登陆，是否存在该用户
     * @param openId
     * @return
     */
    @Override
    public Member existMember(String openId) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openId);
        Member member = baseMapper.selectOne(wrapper);
        return member;
    }

    //获取每天用户注册数量
    @Override
    public int getMemberCount(String day) {

//        baseMapper
        int count=baseMapper.getMember(day);
        return count;
    }
}
